$(function(){

  //navigation
  var isHover = false;
  $(".wrap-nav-ck").hover(function(){
    isHover = true;
    $(this).find(".wrap-nav").fadeIn(200);
  },function(){
    var p = this;
    isHover = false;
    setTimeout(function(){
      if (!isHover) {
        $(p).find(".wrap-nav").fadeOut(200);
      }
    },300);
  });

  //index-switch
  $(".wrap-form p").click(function(){
    var wrap = $(".wrap-form");
    var type = $(this).data("type");
    var parent = $(".index-form");
    var el = parent.find("input[name=type]");

    var tab = wrap.find("p");
    tab.removeClass("sel");
    $(this).addClass("sel");

    if (type == "login") {
      parent.find(".username").hide();
      $(".wrap-formzc").text("快速登录");
      $(".wrap-formlizc").text("立即登录");
    }
    else {
      parent.find(".username").show();
      $(".wrap-formzc").text("免费注册");
      $(".wrap-formlizc").text("立即注册");
    }

    el.val(type);
  });

  $("input").focus(function(){
    $(this).parent().prev().removeClass("error");

    var nextSpan = $(this).next("span");
    var sideSpan = $(this).parent().next("span");
    if (nextSpan.length > 0) nextSpan.hide();
    if (sideSpan.length > 0) sideSpan.hide();
  })

  //表单验证
  $(".wrap-formlizc").click(function(){
    var parent = $(".index-form");
    var type = parent.find("input[name=type]").val();

    var username = parent.find("input[name=username]");
    var usernameVal = username.val();
    var password = parent.find("input[name=password]");
    var passwordVal = password.val();
    var phonemail = parent.find("input[name=phonemail]");
    var phonemailVal = phonemail.val();

    var valid = true;
    //验证手机号和邮箱
    if (!/^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/.test(phonemailVal) &&
      !/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(phonemailVal)) {
      valid = false;
      phonemail.parent().prev().addClass("error");
    }

    //验证密码
    if (passwordVal.length < 6 || passwordVal.length > 16) {
      valid = false;
      password.parent().prev().addClass("error");
    }

    //验证用户名
    if (type == "reg") {
      if (!usernameVal) {
        valid = false;
        username.parent().prev().addClass("error");
      }
    }

    //提交表单
    if (valid) {
      parent.find(".error").removeClass("error");
    }
  });

  //初始化slider
  var mySwiper = new Swiper('.swiper-container',{
    pagination: '.pagination',
    paginationClickable: true,
    autoplay: 3000,
    loop: true
  })


  //报名tab切换
  $(".wrap-detail-baoming a").click(function(){
    var type = $(this).data("type");
    var tab = $(".wrap-detail-baoming a");
    tab.removeClass("sel");
    $(this).addClass("sel");

    $(".wrap-enroll-form").find("input[name=type]").val(type);

    if (type == "team") {
      $(".personal").hide();
      $(".team").show();
    }
    else {
      $(".personal").show();
      $(".team").hide();
    }
  });

  var isPreview = false;
  $(".preview").click(function(){
    if (!isPreview) {
      isPreview = true;
      $("input").addClass("no-decoration").attr("readonly","readonly");
      $("input[type=checkbox]").attr("disabled","disabled");
      $("textarea").addClass("no-decoration").attr("readonly","readonly");
      $("select").attr("disabled","disabled");
      $(".wrap-enroll-shangchuan").hide();
      $(".preview").text("编辑");
    }
    else {
      isPreview = false;
      $("input").removeClass("no-decoration").removeAttr("readonly");
      $("input[type=checkbox]").removeAttr("disabled");
      $("textarea").removeClass("no-decoration").removeAttr("readonly");
      $("select").removeAttr("disabled");
      $(".wrap-enroll-shangchuan").show();
      $(".preview").text("预览");
    }
  })

  $(".wrap-finish-btn.submit").click(function(){
    var form = $(".wrap-enroll-form");
    var type = form.find("input[name=type]").val();

    var formValid = true;

    if (type == "personal") {
      var name = form.find("input[name=name]");
      var idCard = form.find("input[name=idCard]");
      var cardImage = form.find("input[name=cardImage]");
      var phone = form.find("input[name=phone]");
      var healthCertify = form.find("input[name=healthCertify]");
      var contactName = form.find("input[name=contactName]");
      var contactPhone = form.find("input[name=contactPhone]");

      if (!name.val()) {
        getSpan(name).show();
        formValid = false;
      }

      if (!/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(idCard.val())) {
        getSpan(idCard).show();
        formValid = false;
      }

      if (!cardImage.data("valid")) {
        getSpan(cardImage).text("* 请选择正确的图片").removeClass("path").show();
        formValid = false;
      }

      if (!/^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/.test(phone.val())) {
        getSpan(phone).show();
        formValid = false;
      }

      if (!healthCertify.data("valid")) {
        getSpan(healthCertify).text("* 请选择正确的表格").removeClass("path").show();
        formValid = false;
      }

      if (!contactName.val()) {
        getSpan(contactName).show();
        formValid = false;
      }

      if (!/^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/.test(contactPhone.val())) {
        getSpan(contactPhone).show();
        formValid = false;
      }

      if (formValid) {
        //$("span.error").hide();
      }
    }
    else {
      var teamLeader = form.find("input[name=teamLeader]");
      var teamIdCard = form.find("input[name=teamIdCard]");
      var teamCardImage = form.find("input[name=teamCardImage]");
      var teamPhone = form.find("input[name=teamPhone]");
      var teamHealthCertify = form.find("input[name=teamHealthCertify]");
      var teamEnrollBook = form.find("input[name=teamEnrollBook]");

      if (!teamLeader.val()) {
        getSpan(teamLeader).show();
        formValid = false;
      }

      if (!/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(teamIdCard.val())) {
        getSpan(teamIdCard).show();
        formValid = false;
      }

      if (!teamCardImage.data("valid")) {
        getSpan(teamCardImage).text("* 请选择正确的图片").removeClass("path").show();
        formValid = false;
      }

      if (!/^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$/.test(teamPhone.val())) {
        getSpan(teamPhone).show();
        formValid = false;
      }

      if (!teamHealthCertify.data("valid")) {
        getSpan(teamHealthCertify).text("* 请选择正确的表格").removeClass("path").show();
        formValid = false;
      }

      if (!teamEnrollBook.data("valid")) {
        getSpan(teamEnrollBook).text("* 请选择正确的表格").removeClass("path").show();
        formValid = false;
      }

      if (formValid) {
        //$("span.error").hide();
      }
    }
  })

  //倒计时
  $(".countdown").each(function(){
    var totalSeconds = $(this).data("remain-seconds");
    if (totalSeconds) {
      doCountDown(parseInt(totalSeconds), $(this));
    }
  })

})

function doCountDown(intDiff, dom) {
  var intSeed = window.setInterval(function(){
    var day=0,
        hour=0,
        minute=0,
        second=0;//时间默认值
    if(intDiff > 0){
      day = Math.floor(intDiff / (60 * 60 * 24));
      hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
      minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
      second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
    }
    else {
      window.clearInterval(intSeed);
    }

    if (day <= 9) day = '0' + day;
    if (hour <= 9) hour = '0' + hour;
    if (minute <= 9) minute = '0' + minute;
    if (second <= 9) second = '0' + second;

    dom.html("倒计时："+day+" 天 "+hour+" 时 "+minute+" 分 "+second+" 秒");
    intDiff--;

  }, 1000);
}

function getSpan(obj) {
  var span1 = obj.next("span");
  if (span1.length > 0) return span1;
  else {
    return obj.parent().next("span");
  }
}

function setImagePreview(docObj) {
  //var docObj = e.target;
  var fileName = docObj.value;
  if (!fileName.match(/.jpg|.gif|.png|.bmp/i)) {
    $(docObj).data("valid", false);
    alert('您上传的图片格式不正确，请重新选择！');
    return false;
  }
  $(docObj).data("valid", true);
  var imgParent = $(docObj).parent().parent().find(".photo");
  var imgObjPreview = imgParent.find("img")[0];
  if (docObj.files && docObj.files[0]) {
//火狐下，间接设img属性
    imgObjPreview.style.display = 'block';
    //imgObjPreview.style.width = '300px';
    //imgObjPreview.style.height = '188px';
//imgObjPreview.src = docObj.files[0].getAsDataURL();
//    if (window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1) {
//
//    }
//    else {
//      imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
//    }
    imgParent[0].style.background = "none";
    imgObjPreview.src =  window.webkitURL.createObjectURL(docObj.files[0]);
  } else {
//IE下，利用滤镜
    docObj.select();
    docObj.blur();
    var imgSrc = document.selection.createRange().text;
    var localImagId = $(docObj).parent().parent().find(".photo-con")[0];
//必须设备初始
    localImagId.style.width = "300px";
    localImagId.style.height = "188px";
//图片异常的捕捉，避免用户点窜后缀来捏造图片
    try {
      localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
      localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
    } catch (e) {
      alert("您上传的图片格式不正确，请重新挑选！");
      return false;
    }
    imgParent.style.display = 'none';
    document.selection.empty();
  }
  return true;
}

function showFilePath(docObj) {
  var str = $(docObj).val();
  var arr = str.split('\\');//注split可以用字符或字符串分割
  var path = arr[arr.length-1];//这就是要取得的图片名称

  if (!path.match(/.xls|.xlsx/i)) {
    $(docObj).data("valid", false);
    alert("请上传正确的表格");
  }
  else {
    $(docObj).data("valid", true);
    $(docObj).parent().next("span").text(path).addClass("path").show();
  }

}